﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace ProjectC6_GUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            int clock = 0;
            txtTime.Text = DateTime.Now.ToString();
            txtTimecount.Text = clock.ToString(); 
        }
        int intlen = 0,TickStart;
        private void timer1_Tick(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();//put all the ports names to array
            if (intlen != ports.Length)
            {
                intlen = ports.Length;
                CbSecCom.Items.Clear();
                for (int j = 0; j < intlen; j++)
                {
                    CbSecCom.Items.Add(ports[j]);
                }
            }
        }

        private void PbConnect_Click(object sender, EventArgs e)
        {
            if (LbStatus.Text == "Disconnect")
            {
                Com.PortName = CbSecCom.Text;
                Com.Open();
                LbStatus.Text = "Connect";
                PbConnect.Text = "Disconnect";
            }
            else
            {
                Com.Close();
                LbStatus.Text = "Disconnect";
                PbConnect.Text = "Connect";
            }
        }

        private void PbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PbSend_Click(object sender, EventArgs e)
        {
            string s;
            if (LbStatus.Text == "Connect")
            {
                s = txtSend.Text;
                Com.WriteLine(s);
            }
        }

        private void OnCom(object sender, SerialDataReceivedEventArgs e)
        {
            string s;
            s = Com.ReadExisting();
            Display(s);
        }

        private delegate void Dldisplay(string s);
        private void Display(string s)
        {
            if (txtReceive.InvokeRequired)
            {
                Dldisplay sd = new Dldisplay(Display);
                txtReceive.Invoke(sd, new object[] { s });
            }
            else
            {
                txtReceive.Text = s;
                //TxtKp.Text=s;
            }
        }

        private void PbMode_Click(object sender, EventArgs e)
        {
            if (PbMode.Text == "Start")
            {
                TickStart = Environment.TickCount;
                PbMode.Text = "Stop";

            }
            else
            {
                PbMode.Text = "Start";
                double time = (Environment.TickCount - TickStart) / 1000;
                txtTimecount.Text = time.ToString();
            }
        }

     

    

        

      
     
    }
}
